package com.korustar.autumnal.reflect;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Inspection of static fields
 * 
 * @author amir
 *
 */
public class StaticMethodReflection {

	private static final Logger LOGGER = LoggerFactory.getLogger(StaticMethodReflection.class);

	public static List<Class<?>> getArgTypes(String className, String methodName, Integer argCount) {
		Class<?> classFromClasspath = ClassLoaderUtil.getClassFromClasspath(className);
		if(classFromClasspath!=null) {
				//public only ...
				Method[] methods = classFromClasspath.getMethods();
				for (Method method : methods) {
					if(methodName.equals(method.getName())) {
						Class<?>[] parameterTypes = method.getParameterTypes();
						// if null, return this ...
						if(argCount==null || argCount.intValue()==parameterTypes.length) {
							return Arrays.asList(parameterTypes);
						}
					}
				}
		}
		return null;
	}
	
	public static Class<?> getReturnType(String className, String methodName, Class<?>[] args) {
		Class<?> classFromClasspath = ClassLoaderUtil.getClassFromClasspath(className);
		if(classFromClasspath!=null) {
			try {
				Method method = classFromClasspath.getMethod(methodName, args);
				return method.getReturnType();
			} catch (NoSuchMethodException e) {
				LOGGER.warn("No such method {}.{}({})", new Object[]{ className,methodName,args } );
				
			}
		}
		return null;
	}
}
